//
//  ZKJ_PopView.m
//  ZhaoKuaiJi
//
//  Created by 我叫MT on 16/8/5.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "base_PopView.h"

@implementation base_PopView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self yh_setupView];
    }
    return self;
}

-(void)yh_setupView
{
    self.backgroundColor = [UIColor blackColor];
    self.image = [[UIImageView alloc]init];
    self.image.frame = CGRectMake(CGRectGetMidX(self.bounds), CGRectGetMinY(self.bounds), 30, 20);
    _image.image = [UIImage imageNamed:@"success"];
    [self addSubview:_image];
    
    self.message = [[UILabel alloc]init];
    self.message.frame = CGRectMake(CGRectGetMinX(self.bounds)/2, CGRectGetMidY(self.bounds) - 12, self.frame.size.width, 20);
    
    
    _message.textAlignment = NSTextAlignmentCenter;
    _message.textColor = [UIColor whiteColor];
    _message.numberOfLines = 0;
    //_message.backgroundColor = [UIColor blueColor];
    [self addSubview:_message];
    
    
}

+(void)noticesWithTitle:(NSString *)title Time:(NSTimeInterval )time View:(UIView *)view style:(YHNoticesStyle)style{
    
    base_PopView *notices = [[base_PopView alloc]initWithFrame:CGRectMake(0, 0, 0, 30)];
    switch (style) {
        case 0:
            notices.image.image = [UIImage imageNamed:@"success"];
            break;
        case 1:
            notices.image.image = [UIImage imageNamed:@"fail"];
            break;
            
        default:
            break;
    }
    
    notices.layer.masksToBounds = YES;
    notices.layer.cornerRadius = 15;
    notices.dic = [NSDictionary dictionaryWithObject:notices forKey:@"dd"];
    notices.title = title;
    notices.message.font = [UIFont systemFontOfSize:15];
    [view addSubview:notices];
    
    notices.alpha = 0;
    notices.center = CGPointMake(view.bounds.size.width/2 , view.bounds.size.height/2 - 10);
    [UIView animateWithDuration:0 animations:^{
        notices.center = CGPointMake(view.bounds.size.width/2, view.bounds.size.height/2 - 10);
        notices.alpha = 0.7;
    }];
    [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(timeAction:) userInfo:notices.dic repeats:NO];
    
}

+(void)timeAction:(NSTimer *)sender
{
    base_PopView *notices = [[sender userInfo]valueForKey:@"dd"];
    [UIView animateWithDuration:0.5 animations:^{
        notices.alpha = 0;
    } completion:^(BOOL finished) {
        [notices removeFromSuperview];
    }];
    
}
-(void)setTitle:(NSString *)title{
    _title = title;
    self.message.text = _title;
    
    UILabel * label = [[UILabel alloc]init];
    label.text = _title;
    [label sizeToFit];
    self.message.frame = CGRectMake(CGRectGetMinX(self.bounds)/2, ((self.frame.size.height-self.frame.size.height)/2), self.frame.size.width, self.frame.size.height);
}


@end
